﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace YmtAuth.LogService
{
    public class SafeVerifyLog : LogEntityBase
    {
        /// <summary>
        /// 请求接口ID
        /// </summary>
        public int ApiId { get; set; }

        /// <summary>
        /// 请求ID
        /// </summary>
        public string RequestId { get; set; }

        /// <summary>
        /// 安全策略类型; pc, app
        /// </summary>
        public string StrategyType { get; set; }

        /// <summary>
        /// 客户端应用ID（userservice.ymatou.com, etc）或应用分组（App、pc、MainSite, etc）
        /// </summary>
        public string AppSource { get; set; }

        /// <summary>
        /// 用户ID
        /// </summary>
        public int UserId { get; set; }

        /// <summary>
        /// 登录ID
        /// </summary>
        public string LoginId { get; set; }

        /// <summary>
        /// 登录IP
        /// </summary>
        public string LoginIp { get; set; }

        /// <summary>
        /// 服务端Ip
        /// </summary>
        public string ServerIp
        {
            get
            {
                if (string.IsNullOrEmpty(ip))
                    ip = LocalIp();
                return ip;
            }
        }

        private static string ip;
        public string LocalIp()
        {
            var ips = Dns.GetHostEntry(Dns.GetHostName())
                 .AddressList.FirstOrDefault(i => i.AddressFamily == AddressFamily.InterNetwork);
            return ips == null ? null : ips.ToString();
        }

        /// <summary>
        /// 设备号ID（IMEI, IDFA）
        /// </summary>
        public string DeviceId { get; set; }

        /// <summary>
        /// 设备号的标识（由个推服务器产生（通过app上的个推SDK获取），用于向设备推送消息）
        /// </summary>
        public string ClientId { get; set; }

        /// <summary>
        /// 安全认证类型
        /// </summary>
        public string AuthType { get; set; }

        /// <summary>
        /// 验证耗时
        /// </summary>
        public double RunTime { get; set; }

        /// <summary>
        /// 安全认证结果code
        /// </summary>
        public int ResponseCode { get; set; }

        /// <summary>
        /// 安全认证结果描述
        /// </summary>
        public string ResponseDesc { get; set; }


        /// <summary>
        /// IP区域，国家省份城市
        /// </summary>
        public string IpArea { get; set; }

        /// <summary>
        /// IP区域上次认证时间
        /// </summary>
        public DateTime LastAuthTime { get; set; }

        /// <summary>
        /// 黑名单IP过期时间
        /// </summary>
        public DateTime? ExpiredTime { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get { return DateTime.Now; } }

        /// <summary>
        /// 日志类型- 风控日志
        /// </summary>
        public override string LogType
        {
            get
            {
                return "SafeVerifyLog";
            }
        }
    }
}
